package com.finace.money.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.StatusListBean;
import com.finace.money.data.bean.TickBean;
import com.finace.money.interfaces.OnItemClickListener;
import com.finace.money.ui.fragment.MainStatusFragment;
import com.finace.money.ui.widget.CustomHScrollView;

import java.util.List;

/**
 * Created by zhangpeng on 2017/11/27.
 */

public class StatusListViewAdapter extends BaseAdapter {

    private List<StatusListBean> mData;
    private LayoutInflater mInflater;
    private RelativeLayout mHead;
    private int n = 1;//标记变量
    private OnItemClickListener mOnItemClickListener;

    public void setmData(List<StatusListBean> mData) {
        this.mData = mData;
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public StatusListViewAdapter(RelativeLayout head) {
        this.mHead = head;
        this.mInflater = LayoutInflater.from(UIUtil.getContext());
    }

    @Override
    public int getCount() {
        return (null != mData && mData.size() > 0) ? mData.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup group) {
        MyViewHolder holder = null;
        if (view == null) {
            view = mInflater.inflate(R.layout.item_status_list, group, false);
            holder = new MyViewHolder();
            CustomHScrollView scrollView = view.findViewById(R.id.h_scrollView);
            holder.scrollView = scrollView;
            holder.mIvStatus = view.findViewById(R.id.iv_status);
            holder.mTextView11 = view.findViewById(R.id.textView_1_1);
            holder.mTextView1 = view.findViewById(R.id.textView_1);
            holder.mTextView2 = view.findViewById(R.id.textView_2);
            holder.mTextView3 = view.findViewById(R.id.textView_3);
            holder.mTextView4 = view.findViewById(R.id.textView_4);
            holder.mTextView5 = view.findViewById(R.id.textView_5);
            holder.mTextView6 = view.findViewById(R.id.textView_6);
            holder.mTextView7 = view.findViewById(R.id.textView_7);
            holder.mTextView8 = view.findViewById(R.id.textView_8);
            holder.mTextView9 = view.findViewById(R.id.textView_9);
            holder.mRleft = view.findViewById(R.id.rl_left);

            CustomHScrollView headSrcrollView = (CustomHScrollView) mHead.findViewById(R.id.h_scrollView);
            headSrcrollView.AddOnScrollChangedListener(new OnScrollChangedListenerImp(scrollView));

            view.setTag(holder);
        } else {
            holder = (MyViewHolder) view.getTag();
        }
        if (null != mData && mData.size() > 0) {
            StatusListBean.MBodyBean mBody = mData.get(i).getM_Body();
            TickBean tickbean = mBody.getTickbean();
            holder.mTextView1.setText(mBody.getM_ProductName().trim());
            holder.mTextView11.setText(mBody.getM_LocalProductID().trim());

            if (null != tickbean) {

                holder.mTextView2.setText(tickbean.getTrade());
                double zhangdie = Double.valueOf(tickbean.getTrade()) - mBody.getM_PreSettle().getM_Value();
                holder.mTextView3.setText(StringUtil.getDecimal(zhangdie));
                if (zhangdie < 0) {
                    holder.mTextView3.setTextColor(UIUtil.getColor(R.color.colorGreen4CD964));
                } else if (zhangdie > 0) {
                    holder.mTextView3.setTextColor(UIUtil.getColor(R.color.colorRedE51B23));
                } else {
                    holder.mTextView3.setTextColor(UIUtil.getColor(R.color.colorWhite));
                }

                holder.mTextView4.setText(tickbean.getBid());
                holder.mTextView5.setText(tickbean.getAsk());
                holder.mTextView6.setText(StringUtil.getNumberFormat(String.valueOf(zhangdie / mBody.getM_PreSettle().getM_Value())));
                holder.mTextView7.setText(tickbean.getVol());
                holder.mTextView8.setText(tickbean.getHigh());
                holder.mTextView9.setText(tickbean.getLow());

            } else {
                holder.mTextView2.setText("-");
                holder.mTextView3.setText("-");
                holder.mTextView4.setText("-");
                holder.mTextView5.setText("-");
                holder.mTextView6.setText("-");
                holder.mTextView7.setText("-");
                holder.mTextView8.setText("-");
                holder.mTextView9.setText("-");
            }
            if (mData.get(i).getCheck()) {
                holder.mIvStatus.setImageResource(R.drawable.icon_delete);
            } else {
                holder.mIvStatus.setImageResource(R.drawable.icon_add);
            }


        }

        holder.mRleft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onItemClick(view, i);
            }
        });

        return view;
    }

    class OnScrollChangedListenerImp implements CustomHScrollView.OnScrollChangedListener {
        CustomHScrollView mScrollViewArg;

        public OnScrollChangedListenerImp(CustomHScrollView scrollViewar) {
            mScrollViewArg = scrollViewar;
        }

        @Override
        public void onScrollChanged(int l, int t, int oldl, int oldt) {
            mScrollViewArg.smoothScrollTo(l, t);
            if (n == 1) {//记录滚动的起始位置，避免因刷新数据引起错乱
                new MainStatusFragment().setPosData(oldl, oldt);
            }
            n++;
        }
    }

    ;

    class MyViewHolder {
        ImageView mIvStatus;
        TextView mTextView11;
        TextView mTextView1;
        TextView mTextView2;
        TextView mTextView3;
        TextView mTextView4;
        TextView mTextView5;
        TextView mTextView6;
        TextView mTextView7;
        TextView mTextView8;
        TextView mTextView9;
        RelativeLayout mRleft;
        HorizontalScrollView scrollView;
    }
}
